package cn.ekuma.pos.pda.order.local;

import java.util.List;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import cn.ekuma.data.ui.android.AbstractViewActivate;
import cn.ekuma.data.ui.android.BaseBeanListSelecter;
import cn.ekuma.data.ui.android.OnBaseBeanSelectedListener;
import cn.ekuma.epos.datalogic.local.LocalERPDBHelper;
import cn.ekuma.epos.datalogic.local.erp.dao.FindOrdersInfoDAO;
import cn.ekuma.pos.android.erp.R;
import cn.ekuma.pos.pda.order.OrderListAdapter;
import cn.ekuma.pos.pda.qkorder.OrderPanelActivity;
import com.openbravo.bean.erp.Order;
import com.openbravo.bean.erp.viewbean.FindOrdersInfo;

public class LocalOrderViewActivate extends AbstractViewActivate implements OnBaseBeanSelectedListener<FindOrdersInfo>{

	private static final int MSG_SUCCESS = 0;
    private static final int MSG_FAILURE = 1;
    private static final int MSG_LOAD_ORDER_SUCCESS=2;
	OrderListAdapter listAdapter;
	private FindOrdersInfoDAO localOrderDAO;
	
	private static final int CLEAR_ID = Menu.FIRST + 1;
	private static final int DELETE_ID = Menu.FIRST + 2;
	
	
	BaseBeanListSelecter main;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		 LocalERPDBHelper dbHelper=new LocalERPDBHelper(this);
   	     localOrderDAO=new FindOrdersInfoDAO(dbHelper);
   	     for(FindOrdersInfo f:localOrderDAO.list()){
   	    	listAdapter.add(f);
         }	        
   	  registerForContextMenu(main.getListview());
	}

	
	 
	  public boolean onCreateOptionsMenu(Menu menu) {
	    super.onCreateOptionsMenu(menu);
	    if (localOrderDAO.hasItems()) {
	      menu.add(0, CLEAR_ID, 0, R.string.history_clear_text).setIcon(android.R.drawable.ic_menu_delete);
	    }
	    return true;
	  }

	  
	  public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {  
	      case CLEAR_ID:
	        AlertDialog.Builder builder = new AlertDialog.Builder(this);
	        builder.setMessage(R.string.msg_sure);
	        builder.setCancelable(true);
	        builder.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
	          
	          public void onClick(DialogInterface dialog, int i2) {
	        	 localOrderDAO.clear();
	            dialog.dismiss();
	            listAdapter.clear(); 
	          }
	        });
	        builder.setNegativeButton(R.string.button_cancel, null);
	        builder.show();
	        break;
	      default:
	        return super.onOptionsItemSelected(item);
	    }
	    return true;
	  }
	
	  
	  public void onCreateContextMenu(ContextMenu menu,
	                                  View v,
	                                  ContextMenu.ContextMenuInfo menuInfo) {
	    int position =((AdapterView.AdapterContextMenuInfo) menuInfo).position;
	    menu.add(Menu.NONE, position, DELETE_ID, R.string.history_clear_one_history_text);
	    
	  }

	  
	  public boolean onContextItemSelected(MenuItem item) {
	    int position = item.getItemId();
	    FindOrdersInfo temp=listAdapter.getItem(position);
	    switch(item.getOrder()){
	       case DELETE_ID:
	    	   localOrderDAO.delete(temp);
	    	   listAdapter.remove(temp);  
	    }
	    return true;
	  }
	  
	  
	  
	  
	
	public void initAdvSeachView() {
		// TODO Auto-generated method stub
		
	}

	
	public boolean isQuickSeachAble() {
		return false;
	}

	
	public void onQuickSeach(String inputText) {
		// TODO Auto-generated method stub
		
	}

	
	public View createFilterPanel() {
		return null;
	}

	
	public View createAdvFilterPanel() {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	
	public void filterParaReset() {
		// TODO Auto-generated method stub
		
	}  

	
	public View createContentPanel() {
		main=new BaseBeanListSelecter<FindOrdersInfo>(this);
		listAdapter=new OrderListAdapter(this);
		main.setAbstractBaseBeanAdapter(listAdapter);
		main.setOnBaseBeanSelectedListener(this);
		return main;
	}

	
	public void baseBeanSelected(final FindOrdersInfo obj, int position) {
		final ProgressDialog dialog = ProgressDialog.show(this, "", 
		        "查旬中，请稍等 …", true, true);
		new Thread(){
			
			public void run(){
			    Order order=localOrderDAO.load(obj.id);
				mHandler.obtainMessage(MSG_LOAD_ORDER_SUCCESS, order).sendToTarget();
			    dialog.dismiss(); 
		}
		}.start();		
	}

	
	 private Handler mHandler = new Handler() {  
	        public void handleMessage (Message msg) {//此方法在ui线程运行   
	            switch(msg.what) {  
	            case MSG_SUCCESS:  
	            	listAdapter.clear();
					for(FindOrdersInfo info:(List<FindOrdersInfo>)msg.obj)
					listAdapter.add(info);
	                break;  
	            case MSG_FAILURE:  
	                Toast.makeText(getApplication(), "执行出错了", Toast.LENGTH_LONG).show();  
	                break;      
	            case MSG_LOAD_ORDER_SUCCESS:
	            	Intent intent = new Intent();
	            	intent.setClass(LocalOrderViewActivate.this, OrderPanelActivity.class);
	            	Bundle mBundle=new Bundle();
	            	mBundle.putSerializable(OrderPanelActivity.ORDER_TO_SHOW, (Order) msg.obj);
	            	intent.putExtras(mBundle);
	            	startActivity(intent);	
	            	break;
	            }  
	        }  
	    };
}
